import unittest
from Page.userinfo_page import *
from time import sleep
import hashlib
from common.databaseTool import DatabaseTool


class InfoCase(unittest.TestCase):
    def setUp(self):
        # 加载项打开火狐浏览器,自动登录
        self.driver = open_op_browser(path)
        self.up = Userinfo_page(self.driver)
        self.up.open_url(login_url)
        sql = f"update ecs_users set birthday ='1995-07-23',sex ='2', email='761433409@qq.com', qq='761433409',office_phone='8827777',home_phone='8828888',mobile_phone='15180600329',question='我儿时居住地的地址？',answer='王小明' where user_name='lisa'"
        self.db.execute_one(sql)

    @classmethod
    def setUpClass(cls):
        # 连接数据库
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)


    @classmethod
    def tearDownClass(cls):
        # 还原数据库
        sql = f"update ecs_users set birthday ='1995-07-23',sex ='2', email='761433409@qq.com', qq='761433409',office_phone='8827777',home_phone='8828888',mobile_phone='15180600329',question='我儿时居住地的地址？',answer='王小明' where user_name='lisa'"
        cls.db.execute_one(sql)

    def tearDown(self):
        self.up.close()

    # 修改生日-年
    def test_update_userinfo_case01(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入出生日期-年份
        self.up.birthday_year("1994")
        # 点击确认修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select birthday from ecs_users where user_name ='lisa'"
            birthday = str(self.db.fetchOne(sql)["birthday"])
            self.assertIn("1994", birthday, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 修改生日 -月
    def test_update_userinfo_case02(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入出生日期-月份
        self.up.birthday_month("03")
        # 点击确认修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select birthday from ecs_users where user_name ='lisa'"
            birthday = str(self.db.fetchOne(sql)["birthday"])
            self.assertIn("03", birthday, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 修改生日 -日
    def test_update_userinfo_case03(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入出生日期-day
        self.up.birthday_day("13")
        # 点击确认修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select birthday from ecs_users where user_name ='lisa'"
            birthday = str(self.db.fetchOne(sql)["birthday"])
            self.assertIn("13", birthday, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 修改性别
    def test_update_userinfo_case04(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入性别  男 女  保密
        self.up.sex("2")
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select sex from ecs_users where user_name ='lisa'"
            sex = str(self.db.fetchOne(sql)["sex"])
            self.assertEqual("2", sex, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 邮箱为空 有弹框
    def test_update_userinfo_case05(self):
        user_email1 = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入邮箱
        self.up.input_email(user_email1)
        # 点击修改按钮
        self.up.userinfo_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("请输入您的电子邮件地址！", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")

    # 修改 邮箱 格式不正确
    def test_update_userinfo_case06(self):
        user_email1 = "9999"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入邮箱
        self.up.input_email(user_email1)
        # 点击修改按钮
        self.up.userinfo_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("格式不正确", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")


    # 修改 邮箱
    def test_update_userinfo_case07(self):
        user_email2 = "23452@qq.com"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入邮箱
        self.up.input_email(user_email2)
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select email from ecs_users where user_name ='lisa'"
            email = str(self.db.fetchOne(sql)["email"])
            self.assertEqual(user_email2, email, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 判断qq为空可以保存
    def test_update_userinfo_case08(self):
        qq_num = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入qq
        self.up.input_qq_number(qq_num)
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select qq from ecs_users where user_name ='lisa'"
            qq_n = str(self.db.fetchOne(sql)["qq"])
            self.assertEqual(qq_n, qq_num, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 判断qq非纯数字不可以保存
    def test_update_userinfo_case9(self):
        qq_num = "123哈456"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入qq
        self.up.input_qq_number(qq_num)
        # 点击修改按钮
        self.up.userinfo_enter()
        sleep(5)
        tos = self.up.get_element_text(("xpath", "/html/body/div[5]/div/div/div/div/p[1]"))
        try:
            # 断言qq有误不能保存
            self.assertIn('QQ号码不是一个有效的号码', tos, msg="用例执行失败")
            sql = "select qq from ecs_users where user_name ='lisa'"
            # 判断数据库内的qq没有改变
            qq_n = str(self.db.fetchOne(sql)["qq"])
            self.assertEqual(qq_n, "761433409", msg="qq号不一致")
        except Exception as e:
            raise ("用例执行失败")

    # 判断办公电话为空可以保存
    def test_update_userinfo_case10(self):
        office_tel = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入qq
        self.up.input_office_tel(office_tel)
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select office_phone from ecs_users where user_name ='lisa'"
            office_phone = str(self.db.fetchOne(sql)["office_phone"])
            self.assertEqual(office_phone, office_tel, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 判断办公电话非纯数字不可以保存
    def test_update_userinfo_case11(self):
        office_phone = "8123哈456"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入办公电话
        self.up.input_office_tel(office_phone)
        # 点击修改按钮
        self.up.userinfo_enter()
        sleep(5)
        tos = self.up.get_element_text(("xpath", "/html/body/div[5]/div/div/div/div/p[1]"))
        try:
            # 断言办公电话有误不能保存
            self.assertIn('办公电话不是一个有效号码', tos, msg="用例执行失败")
            sql = "select office_phone from ecs_users where user_name ='lisa'"
            # 判断数据库内的办公电话没有改变
            office_phone = str(self.db.fetchOne(sql)["office_phone"])
            self.assertEqual(office_phone, "8827777", msg="办公电话号不一致")
        except Exception as e:
            raise ("用例执行失败")

    # 判断家庭电话为空可以保存
    def test_update_userinfo_case12(self):
        home_tel = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入qq
        self.up.input_home_tel(home_tel)
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select home_phone from ecs_users where user_name ='lisa'"
            home_phone = str(self.db.fetchOne(sql)["home_phone"])
            self.assertEqual(home_phone, home_tel, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 判断家庭电话非纯数字不可以保存
    def test_update_userinfo_case13(self):
        home_phone = "8123哈456"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入办公电话
        self.up.input_home_tel(home_phone)
        # 点击修改按钮
        self.up.userinfo_enter()
        sleep(5)
        tos = self.up.get_element_text(("xpath", "/html/body/div[5]/div/div/div/div/p[1]"))
        try:
            # 断言家庭电话有误不能保存
            self.assertIn('家庭电话不是一个有效号码', tos, msg="用例执行失败")
            sql = "select home_phone from ecs_users where user_name ='lisa'"
            # 判断数据库内的家庭电话没有改变
            home_phone = str(self.db.fetchOne(sql)["home_phone"])
            self.assertEqual(home_phone, "8828888", msg="办公电话号不一致")
        except Exception as e:
            raise ("用例执行失败")

    # 判断手机号为非纯数字不可以保存
    def test_update_userinfo_case14(self):
        mobile = "8123哈456"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入手机号码
        self.up.tel_num(mobile)
        # 点击修改按钮
        self.up.userinfo_enter()
        sleep(5)
        tos = self.up.get_element_text(("xpath", "/html/body/div[5]/div/div/div/div/p[1]"))
        try:
            # 断言电话有误不能保存
            self.assertIn('手机号码不是一个有效号码', tos, msg="用例执行失败")
            sql = "select mobile_phone from ecs_users where user_name ='lisa'"
            # 判断数据库内的办公电话没有改变
            mobile_phone = str(self.db.fetchOne(sql)["mobile_phone"])
            self.assertEqual(mobile_phone, "15180600329", msg="办公电话号不一致")
        except Exception as e:
            raise ("用例执行失败")

    # 修改 手机号码 为空 有弹框提示
    def test_update_userinfo_case15(self):
        mobile = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入邮箱
        self.up.tel_num(mobile)
        # 点击修改按钮
        self.up.userinfo_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("手机：不能为空", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")

    # 修改 手机号码
    def test_update_userinfo_case16(self):
        tel = "15180600441"
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入手机号
        self.up.tel_num(tel)
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select mobile_phone from ecs_users where user_name ='lisa'"
            mobile_phone = str(self.db.fetchOne(sql)["mobile_phone"])
            self.assertEqual(tel, mobile_phone, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 修改 密保问题  这条会失败  一个bug
    def test_update_userinfo_case17(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 选择密保问题的索引, question为int类型
        self.up.question("我最好朋友的生日？")
        # 获取定位到的问题文本
        que = self.up.get_element_text(("css selector", "option[value='friend_birthday']"))
        print(que)
        # 输入密保问题答案
        self.up.answer("问题2的答案")
        # 点击修改按钮
        self.up.userinfo_enter()
        sleep(10)
        try:
            sql = "select question from ecs_users where user_name ='lisa'"
            question = str(self.db.fetchOne(sql)["question"])
            self.assertEqual(que, question, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 修改 密保问题答案  这条会失败 无法执行 数据库内没有密保问题 是个bug
    def test_update_userinfo_case18(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 选择密保问题,
        self.up.question("我最好朋友的生日？")
        # 输入密保问题答案
        self.up.answer("问题1的答案")
        # 点击修改按钮
        self.up.userinfo_enter()
        try:
            sql = "select answer from ecs_users where user_name ='lisa'"
            answer = str(self.db.fetchOne(sql)["answer"])
            self.assertEqual("问题1的答案", answer, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 输入合法数据 修改用户信息 输入的和数据库内的对比
    #  这条会报错  密保问题
    def test_update_userinfo_case19(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入出生日期-年份
        self.up.birthday_year("1994")
        # 输入出生日期-月份
        self.up.birthday_month("03")
        # 输入出生日期-day
        self.up.birthday_day("13")
        # 输入性别  男 女  保密
        self.up.sex("1")
        # 输入邮箱
        self.up.input_email("23451@qq.com")
        # 输入qq
        self.up.input_qq_number("287422")
        # 输入办公电话
        self.up.input_office_tel("2748921")
        # 输入家庭电话
        self.up.input_home_tel("9419823")
        # 输入手机号
        self.up.tel_num("4918741")
        # 选择密保问题的索引, question为int类型
        self.up.question("我最好朋友的生日？")
        que = self.up.get_element_text(("css selector", "option[value='friend_birthday']"))
        # 输入密保问题答案
        self.up.answer("dhsh")
        self.up.userinfo_enter()
        try:
            sql = "select birthday,sex,email,qq,office_phone,home_phone,mobile_phone,question,answer from ecs_users where user_name ='lisa'"
            birthday = str(self.db.fetchOne(sql)["birthday"])
            sex = str(self.db.fetchOne(sql)["sex"])
            email = str(self.db.fetchOne(sql)["email"])
            qq_n = str(self.db.fetchOne(sql)["qq"])
            office_phone = str(self.db.fetchOne(sql)["office_phone"])
            home_phone = str(self.db.fetchOne(sql)["home_phone"])
            mobile_phone = str(self.db.fetchOne(sql)["mobile_phone"])
            question = str(self.db.fetchOne(sql)["question"])
            answer = str(self.db.fetchOne(sql)["answer"])
            self.assertEqual("1994" + "-" + "03" + "-" + "13", birthday, msg="用例执行失败")
            self.assertEqual("1", sex, msg="用例执行失败")
            self.assertEqual("23451@qq.com", email, msg="用例执行失败")
            self.assertEqual("287422", qq_n, msg="qq号不一致")
            self.assertEqual("2748921", office_phone, msg="办公电话号不一致")
            self.assertEqual("9419823", home_phone, msg="办公电话号不一致")
            self.assertEqual("4918741", mobile_phone, msg="用例执行失败")
            self.assertEqual(que, question, msg="用例执行失败")
            self.assertEqual("问题1的答案", answer, msg="用例执行失败")
        except Exception as e:
            raise ("用例执行失败")

    # 修改密码  不输入原始密码 有弹框提示
    def test_update_userinfo_case20(self):
        old_pd = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入旧密码
        self.up.input_old_password(old_pd)
        # 输入新密码
        self.up.input_new_password("000000")
        # 输入确认新密码
        self.up.input_chick_password("000000")
        # 点击修改按钮
        self.up.password_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("请输入您的原密码！", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")

    # 修改密码  不输入新密码 有弹框提示
    def test_update_userinfo_case21(self):
        new_pd = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入旧密码
        self.up.input_old_password("123456")
        # 输入新密码
        self.up.input_new_password(new_pd)
        # 输入确认新密码
        self.up.input_chick_password("000000")
        # 点击修改按钮
        self.up.password_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("请输入您的新密码！", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")

    # 修改密码  不输入确认密码 有弹框提示
    def test_update_userinfo_case22(self):
        chick_pd = ""
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入旧密码
        self.up.input_old_password("123456")
        # 输入新密码
        self.up.input_new_password("00000")
        # 输入确认新密码
        self.up.input_chick_password(chick_pd)
        # 点击修改按钮
        self.up.password_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("请输入您的确认密码！", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")

    # 判断新密码小于6位数不可以保存
    def test_update_userinfo_case23(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入旧密码
        self.up.input_old_password("123456")
        # 输入新密码
        self.up.input_new_password("00000")
        # 输入确认新密码
        self.up.input_chick_password("00000")
        # 点击修改按钮
        self.up.password_enter()
        sleep(5)
        tos = self.up.get_element_text(("xpath", "/html/body/div[5]/div/div/div/div/p[1]"))
        try:
            # 断言新密码小于6位数不能保存
            self.assertIn(' 登录密码不能少于 6 个字符', tos, msg="用例执行失败")
            # 使用原密码重新登录 检测能否重新登录
            from Page.login_page import Login_page
            driver = open_browser()
            lg = Login_page(driver)
            lg.open_url("http://localhost:8080/ecshop/user.php")
            lg.send_text(("name", "username"), "lisa")
            lg.send_text(("name", "password"), "123456")
            lg.click(("name", "submit"))
            user = lg.get_element_text(("class name", "f4_b"))
            self.assertEqual(user, "lisa", msg="用例执行失败,当前页面登录用户名与预期不一致")
        except Exception as e:
            raise ("用例执行失败")

    # 判断新密码和确认密码不一致不可以保存
    def test_update_userinfo_case24(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入旧密码
        self.up.input_old_password("123456")
        # 输入新密码
        self.up.input_new_password("000000")
        # 输入确认新密码
        self.up.input_chick_password("000001")
        # 点击修改按钮
        self.up.password_enter()
        # 处理弹窗
        al = self.driver.switch_to.alert
        try:
            self.assertIn("您现两次输入的密码不一致！", al.text, msg="用例执行失败")
            al.accept()
        except Exception as e:
            raise ("用例执行失败")

    # 判断新密码和确认密码不一致不可以保存
    def test_update_userinfo_case25(self):
        # 在页面内点击用户信息进入个人信息修改页面
        self.up.click_userinfo()
        # 输入旧密码
        self.up.input_old_password("345678")
        # 输入新密码
        self.up.input_new_password("123456")
        # 输入确认新密码
        self.up.input_chick_password("123456")
        # 点击修改按钮
        self.up.password_enter()

        # 使用新密码重新登录 检测能否重新登录
        from Page.login_page import Login_page
        driver = open_browser()
        lg = Login_page(driver)
        lg.open_url("http://localhost:8080/ecshop/user.php")
        lg.send_text(("name", "username"), "lisa")
        lg.send_text(("name", "password"), "123456")
        lg.click(("name", "submit"))
        user = lg.get_element_text(("class name", "f4_b"))
        try:
            self.assertEqual(user, "lisa", msg="用例执行失败,当前页面登录用户名与预期不一致")
        except Exception as e:
            raise ("用例执行失败")
        # 使用旧密码重新登录 检测能否重新登录
        from Page.login_page import Login_page
        driver = open_browser()
        lg = Login_page(driver)
        lg.open_url("http://localhost:8080/ecshop/user.php")
        lg.send_text(("name", "username"), "lisa")
        lg.send_text(("name", "password"), "345678")
        lg.click(("name", "submit"))
        try:
            user = lg.get_element_text(("class name", "f4_b"))
        except Exception as e:
            pass
        else:
            self.assertTrue(False, msg="用例执行失败")

        sql = "delete from `ecs_users` where user_name='lisa'"
        self.db.execute_one(sql)
        pd = hashlib.md5(b"123456").hexdigest()
        sql2 = f"insert into ecs_users (user_name,`email`,`password`,`mobile_phone`) values ('lisa','761433409@qq.com','{pd}','15180600329')"
        self.db.execute_one(sql2)


if __name__ == '__main__':
    unittest.main()
